package cn.edu.fzu.delivery.service.impl;

import cn.edu.fzu.delivery.domain.entity.CourierDelivery;
import cn.edu.fzu.delivery.domain.entity.NetWorkDay;
import cn.edu.fzu.delivery.domain.enums.DeliveryStatusEnum;
import cn.edu.fzu.delivery.exception.ServiceException;
import cn.edu.fzu.delivery.mapper.CourierDeliveryMapper;
import cn.edu.fzu.delivery.service.CourierDeliveryService;
import cn.edu.fzu.delivery.service.NetworkService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author jiebin
 * @data 2022/11/13
 **/
@Service
public class CourierDeliveryServiceImpl implements CourierDeliveryService {

    private static final long     WHOLE_DAY = 24 * 60 * 60 * 1000L;

    @Resource
    private CourierDeliveryMapper courierDeliveryMapper;
    @Resource
    private NetworkService        networkService;

    @Override
    public void updateStatus(Long deliveryManId, String courierId, DeliveryStatusEnum status, String endTime) {
        courierDeliveryMapper.updateStatus(deliveryManId, courierId, status, endTime);
        if (status == DeliveryStatusEnum.DELIVERED) {

            //自动优化派送路径的时间
            CourierDelivery courierDelivery = getByCourierIdAndDeliveryManId(courierId, deliveryManId);
            if (courierDelivery.getDestNetWork() == null) {
                return;
            }
            int day = getDiffDay(courierDelivery.getCreateTime(), courierDelivery.getEndTime());

            Long startNetworkId = courierDelivery.getDeliveryMan().getNetwork().getId();
            Long endNetworkId = courierDelivery.getDestNetWork().getId();
            List<NetWorkDay> netWorkDays = networkService.listNetWorkDay(startNetworkId, endNetworkId, null);
            if (CollectionUtils.isEmpty(netWorkDays)) {
                //若没有路径，则添加路径
                networkService.addNetworkDay(startNetworkId, endNetworkId,
                        getDiffDay(courierDelivery.getCreateTime(), courierDelivery.getEndTime()));
            } else {
                NetWorkDay netWorkDay = netWorkDays.get(0);
                //取平均值，若天数不足一天按一天算，如：1.5天 则为 2 天
                int average = 0;
                if ((netWorkDay.getDay() + day) % 2 == 0) {
                    average = (netWorkDay.getDay() + day) / 2;
                } else {
                    average = (netWorkDay.getDay() + day) / 2 + 1;
                }
                networkService.updateNetworkDay(netWorkDay.getId(), startNetworkId, endNetworkId, average);
            }

        }
    }


    @Override
    public void insert(String courierId, Long deliveryManId, Long destNetWork) {
        CourierDelivery courierDelivery = this.getByCourierIdAndDeliveryManId(courierId, deliveryManId);
        if (courierDelivery != null) {
            throw new ServiceException("该快递员已经绑定了该快递");
        }
        courierDeliveryMapper.insert(courierId, deliveryManId, destNetWork);
    }


    @Override
    public List<CourierDelivery> list(Long deliveryManId, String courierId, DeliveryStatusEnum status,
                                      Long destNetWorkId, String startTime, String endTime) {
        return courierDeliveryMapper.list(deliveryManId, courierId, status, destNetWorkId, startTime, endTime);
    }


    @Override
    public CourierDelivery getByCourierIdAndDeliveryManId(String courierId, Long deliveryManId) {
        List<CourierDelivery> list = courierDeliveryMapper.list(deliveryManId, courierId, null, null, null, null);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }


    @Override
    public List<String> listCourierIdsByNetWork(Long destNetWorkId) {
        return courierDeliveryMapper.listCourierIdsByNetWork(destNetWorkId);
    }


    private int getDiffDay(Date start, Date end) {
        long elapse = end.getTime() - start.getTime();
        return (int) (elapse / WHOLE_DAY + 1);
    }
}
